package com.RKB.web;

import com.RKB.pojo.answerType;
import com.RKB.service.AnswerResultService;
import com.RKB.service.questionbankService;
import com.RKB.service.threeIdSercive;
import com.RKB.api.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
public class ThreeIdController {

    @Autowired
    private threeIdSercive threeIdsercive;
    @Autowired
    private questionbankService questionbankservice;
    @Autowired
    private AnswerResultService answerResultService;

    private Integer score=0;


//    功能存疑
    @RequestMapping("/findSimulationAccuracyId")
    public AjaxResult<List> findSimulationAccuracyId(){
        AjaxResult<List> result = new AjaxResult<>();
        List<Integer> allId = threeIdsercive.findAllId("simulation_Accuracy_Id", "root");
        result.setCode(200);
        result.setMsg("success");
        result.setData(allId);
        return result;
    }


    @RequestMapping("/addSimulationAccuracyId")
    public AjaxResult<List> addSimulationAccuracyId(@RequestBody List<answerType> list){
        ArrayList<Integer> error = new ArrayList<>();
        AjaxResult<List> result = new AjaxResult<>();

        Map<Integer, String> userCorrectMap = list.stream().
                collect(Collectors.toMap(answerType::getId, answerType::getCorrect));  //  用户答题结果list转换为map

        Set<Integer> userCorrectMapKeys = userCorrectMap.keySet(); //  用户答题结果的keys
        List<answerType> correct = questionbankservice.findCorrect();  //  查询题库全部题的正确答案

        Map<Integer, String> trueCorrectMap = correct.stream().
                collect(Collectors.toMap(answerType::getId, answerType::getCorrect));  //  题库所有答案list转换为map

        //System.out.println(trueCorrectMap);
        // System.out.println(userCorrectMap);  {1=A, 2=B}
        // System.out.println(integers);    [1, 2]

        Iterator<Integer> userCorrectMapKeysIterator = userCorrectMapKeys.iterator();  //用户答题结果迭代器
        int id;
        String allType;
        int AllTypeNum;
        for (int i = 0; i < userCorrectMapKeys.size(); i++) {
            id = userCorrectMapKeysIterator.next();
            if (!trueCorrectMap.get(id).equals(userCorrectMap.get(id))) {
                error.add(id);
                threeIdsercive.insertId("root","simulation_Accuracy_Id",id);  // 往数据库中加入错题的id;
            }else{
                score++;
            }
            String typeById = questionbankservice.findTypeById(id);
            //System.out.println(typeById);
            if(typeById.equals("嵌入式中级-开发设计")){
                typeById="DevelopmentAndDesign";
            }
            if(typeById.equals("嵌入式中级-存储系统")){
                typeById="StorageSystem";
            }
            if(typeById.equals("嵌入式中级-指令系统")){
                typeById="InstructionSystem";
            }
            if(typeById.equals("嵌入式中级-操作系统")){
                typeById="OperatingSystem";
            }
            if(typeById.equals("嵌入式中级-多媒体")){
                typeById="Multimedia";
            }
            if(typeById.equals("嵌入式中级-计算机基础")){
                typeById="FundamentalsOfComputer";
            }

            allType = typeById+"_num";
            AllTypeNum = answerResultService.findErrorNum(allType, "root")+1;
            answerResultService.updataErrorNum(allType,AllTypeNum,"root");
        }
        threeIdsercive.insertRate("simulation_Accuracy_Rate","root",userCorrectMapKeys.size()-score,userCorrectMapKeys.size());


        int errorNum;
        // System.out.println("error = "+error);    //这是错题的id  error是一个list集合;
        for (int i = 0; i < error.size(); i++) {
            String errorType = questionbankservice.findTypeById(error.get(i));
            //System.out.println(errorType);
            if(errorType.equals("嵌入式中级-开发设计")){
                errorType="DevelopmentAndDesign";
            }
            if(errorType.equals("嵌入式中级-存储系统")){
                errorType="StorageSystem";
            }
            if(errorType.equals("嵌入式中级-指令系统")){
                errorType="InstructionSystem";
            }
            if(errorType.equals("嵌入式中级-操作系统")){
                errorType="OperatingSystem";
            }
            if(errorType.equals("嵌入式中级-多媒体")){
                errorType="Multimedia";
            }
            if(errorType.equals("嵌入式中级-计算机基础")){
                errorType="FundamentalsOfComputer";
            }
            //System.out.println(errorType);
            //System.out.println(ErrorNum);
            //System.out.println(ErrorNum.equals("StorageSystem_num"));
            //Integer errorNum = answerResultService.findErrorNum(ErrorNum,"root");
            //System.out.println(errorNum);
            errorNum = answerResultService.findErrorTypeScore(errorType,"root")+1;
            answerResultService.updataErrorTypeScore(errorType,errorNum,"root");
        }

        result.setCode(200);
        result.setMsg("success");
        result.setData(null);
        return result;
    }


    @RequestMapping("/findExerciseAccuracyId")
    public AjaxResult<List> findExerciseAccuracyId(){
        AjaxResult<List> result = new AjaxResult<>();
        List<Integer> allId = threeIdsercive.findAllId("exercise_Accuracy_Id", "root");
        result.setCode(200);
        result.setMsg("success");
        result.setData(allId);
        return result;
    }

    @RequestMapping("/addExerciseAccuracyId")
    public AjaxResult<List> addExerciseAccuracyId(@RequestBody List<answerType> list){
        ArrayList<Integer> error = new ArrayList<>();
        AjaxResult<List> result = new AjaxResult<>();

        Map<Integer, String> userCorrectMap = list.stream().
                collect(Collectors.toMap(answerType::getId, answerType::getCorrect));  //  用户答题结果list转换为map

        Set<Integer> userCorrectMapKeys = userCorrectMap.keySet(); //  用户答题结果的keys
        List<answerType> correct = questionbankservice.findCorrect();  //  查询题库全部题的正确答案

        Map<Integer, String> trueCorrectMap = correct.stream().
                collect(Collectors.toMap(answerType::getId, answerType::getCorrect));  //  题库所有答案list转换为map

        //System.out.println(trueCorrectMap);
        // System.out.println(userCorrectMap);  {1=A, 2=B}
        // System.out.println(integers);    [1, 2]

        Iterator<Integer> userCorrectMapKeysIterator = userCorrectMapKeys.iterator();  //用户答题结果迭代器
        int id;
        String allType;
        int AllTypeNum;
        for (int i = 0; i < userCorrectMapKeys.size(); i++) {
            id = userCorrectMapKeysIterator.next();
            if (!trueCorrectMap.get(id).equals(userCorrectMap.get(id))) {
                error.add(id);
                threeIdsercive.insertId("root","exercise_Accuracy_Id",id);  // 往数据库中加入错题的id;
            }else{
                score++;
            }
            String typeById = questionbankservice.findTypeById(id);
            //System.out.println(typeById);
            if(typeById.equals("嵌入式中级-开发设计")){
                typeById="DevelopmentAndDesign";
            }
            if(typeById.equals("嵌入式中级-存储系统")){
                typeById="StorageSystem";
            }
            if(typeById.equals("嵌入式中级-指令系统")){
                typeById="InstructionSystem";
            }
            if(typeById.equals("嵌入式中级-操作系统")){
                typeById="OperatingSystem";
            }
            if(typeById.equals("嵌入式中级-多媒体")){
                typeById="Multimedia";
            }
            if(typeById.equals("嵌入式中级-计算机基础")){
                typeById="FundamentalsOfComputer";
            }
            allType = typeById+"_num";
            AllTypeNum = answerResultService.findErrorNum(allType, "root")+1;
            answerResultService.updataErrorNum(allType,AllTypeNum,"root");
        }
        threeIdsercive.insertRate("exercise_Accuracy_Rate","root",userCorrectMapKeys.size()-score,userCorrectMapKeys.size());


        int errorNum;
        // System.out.println("error = "+error);    //这是错题的id  error是一个list集合;
        for (int i = 0; i < error.size(); i++) {
            String errorType = questionbankservice.findTypeById(error.get(i));
            //System.out.println(errorType);
            if(errorType.equals("嵌入式中级-开发设计")){
                errorType="DevelopmentAndDesign";
            }
            if(errorType.equals("嵌入式中级-存储系统")){
                errorType="StorageSystem";
            }
            if(errorType.equals("嵌入式中级-指令系统")){
                errorType="InstructionSystem";
            }
            if(errorType.equals("嵌入式中级-操作系统")){
                errorType="OperatingSystem";
            }
            if(errorType.equals("嵌入式中级-多媒体")){
                errorType="Multimedia";
            }
            if(errorType.equals("嵌入式中级-计算机基础")){
                errorType="FundamentalsOfComputer";
            }
            //System.out.println(errorType);
            //System.out.println(ErrorNum);
            //System.out.println(ErrorNum.equals("StorageSystem_num"));
            //Integer errorNum = answerResultService.findErrorNum(ErrorNum,"root");
            //System.out.println(errorNum);
            errorNum = answerResultService.findErrorTypeScore(errorType,"root")+1;
            answerResultService.updataErrorTypeScore(errorType,errorNum,"root");
        }

        result.setCode(200);
        result.setMsg("success");
        result.setData(null);
        return result;
    }

    @RequestMapping("/findTestAccuracyId")
    public AjaxResult<List> findTestAccuracyId(){
        AjaxResult<List> result = new AjaxResult<>();
        List<Integer> allId = threeIdsercive.findAllId("test_Accuracy_Id", "root");
        result.setCode(200);
        result.setMsg("success");
        result.setData(allId);
        return result;
    }
    @RequestMapping("/addTestAccuracyId")
    public AjaxResult<List> addTestAccuracyId(@RequestBody List<answerType> list){
        ArrayList<Integer> error = new ArrayList<>();
        AjaxResult<List> result = new AjaxResult<>();

        Map<Integer, String> userCorrectMap = list.stream().
                collect(Collectors.toMap(answerType::getId, answerType::getCorrect));  //  用户答题结果list转换为map

        Set<Integer> userCorrectMapKeys = userCorrectMap.keySet(); //  用户答题结果的keys
        List<answerType> correct = questionbankservice.findCorrect();  //  查询题库全部题的正确答案

        Map<Integer, String> trueCorrectMap = correct.stream().
                collect(Collectors.toMap(answerType::getId, answerType::getCorrect));  //  题库所有答案list转换为map

        //System.out.println(trueCorrectMap);
        // System.out.println(userCorrectMap);  {1=A, 2=B}
        // System.out.println(integers);    [1, 2]

        Iterator<Integer> userCorrectMapKeysIterator = userCorrectMapKeys.iterator();  //用户答题结果迭代器
        int id;
        String allType;
        int AllTypeNum;
        for (int i = 0; i < userCorrectMapKeys.size(); i++) {
            id = userCorrectMapKeysIterator.next();
            if (!trueCorrectMap.get(id).equals(userCorrectMap.get(id))) {
                error.add(id);
                threeIdsercive.insertId("root","test_Accuracy_Id",id);  // 往数据库中加入错题的id;
            }else{
                score++;
            }
            String typeById = questionbankservice.findTypeById(id);
            //System.out.println(typeById);
            if(typeById.equals("嵌入式中级-开发设计")){
                typeById="DevelopmentAndDesign";
            }
            if(typeById.equals("嵌入式中级-存储系统")){
                typeById="StorageSystem";
            }
            if(typeById.equals("嵌入式中级-指令系统")){
                typeById="InstructionSystem";
            }
            if(typeById.equals("嵌入式中级-操作系统")){
                typeById="OperatingSystem";
            }
            if(typeById.equals("嵌入式中级-多媒体")){
                typeById="Multimedia";
            }
            if(typeById.equals("嵌入式中级-计算机基础")){
                typeById="FundamentalsOfComputer";
            }
            allType = typeById+"_num";
            AllTypeNum = answerResultService.findErrorNum(allType, "root")+1;
            answerResultService.updataErrorNum(allType,AllTypeNum,"root");
        }

        threeIdsercive.insertRate("test_Accuracy_Rate","root",userCorrectMapKeys.size()-score,userCorrectMapKeys.size());

        int errorNum;
        // System.out.println("error = "+error);    //这是错题的id  error是一个list集合;
        for (int i = 0; i < error.size(); i++) {
            String errorType = questionbankservice.findTypeById(error.get(i));
            //System.out.println(errorType);
            if(errorType.equals("嵌入式中级-开发设计")){
                errorType="DevelopmentAndDesign";
            }
            if(errorType.equals("嵌入式中级-存储系统")){
                errorType="StorageSystem";
            }
            if(errorType.equals("嵌入式中级-指令系统")){
                errorType="InstructionSystem";
            }
            if(errorType.equals("嵌入式中级-操作系统")){
                errorType="OperatingSystem";
            }
            if(errorType.equals("嵌入式中级-多媒体")){
                errorType="Multimedia";
            }
            if(errorType.equals("嵌入式中级-计算机基础")){
                errorType="FundamentalsOfComputer";
            }
            //System.out.println(errorType);
            //System.out.println(ErrorNum);
            //System.out.println(ErrorNum.equals("StorageSystem_num"));
            //Integer errorNum = answerResultService.findErrorNum(ErrorNum,"root");
            //System.out.println(errorNum);
            errorNum = answerResultService.findErrorTypeScore(errorType,"root")+1;
            answerResultService.updataErrorTypeScore(errorType,errorNum,"root");
        }

        result.setCode(200);
        result.setMsg("success");
        result.setData(null);
        return result;
    }

    @RequestMapping("/getScore")
    public AjaxResult<Integer> getScore(){
        AjaxResult<Integer> result = new AjaxResult<>();
        result.setCode(200);
        result.setMsg("success");
        result.setData(score);
        score=0;
        return result;
    }
}
